package gspushmq.v1.produce.job.core;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import ggframework.mongodb.BasicDBObject;
import ggframework.mongodb.DBCollection;
import ggframework.mongodb.DBObject;
import gspushmq.util.GGLogger;
import gspushmq.util.MongoUtil;

public class GGServiceLogDBUtil {

	private static final String HQ_LOG_DB = "gg_job_log";

	public static final void log(String type, Class<?> clazz, Object... message) {
		log(type, clazz, null, null, message);
	}
	
	public static final void log(String type, String cornExpression, Class<?> clazz, Map<?, ?> param, Object... message) {
		DBCollection coll = MongoUtil.getGGStockBaseCollection(HQ_LOG_DB);
		DBObject o = new BasicDBObject("type", type).append("class", clazz.getName()).append("param", param)
				.append("insert_time", new Date());
		try {
//			Method method2 = clazz.getMethod("getCorn");
//			Object obj2 = method2.invoke(clazz.newInstance());
			o.append("job_corn", cornExpression);
			Method method1 = clazz.getMethod("getJobName");
			Object obj1 = method1.invoke(clazz.newInstance());
			o.append("job_name", obj1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			o.append("job_name", null);
		}
		if (message != null && message.length > 0) {
			for (int i = 0; i < message.length; i++) {
				Object obj = message[i];
				if (obj instanceof Throwable) {
					obj = getStackTrace((Throwable) obj);
				}
				o.append("message_" + i, obj);
			}
		}
		coll.insert(o);
	}

	public static void debug(Class<?> clazz, Object... message) {
		log("debug", clazz, message);
	}

	public static void error(Class<?> clazz, Object... message) {
		log("error", clazz, message);
	}

	public static void info(Class<?> clazz, Object... message) {
		log("info", clazz, message);
	}

	public static void debug(Class<?> clazz, String cornExpression, Map<?, ?> param, Object... message) {
		log("debug", cornExpression, clazz, param, message);
	}

	public static void error(Class<?> clazz, String cornExpression, Map<?, ?> param, Object... message) {
		log("error", cornExpression, clazz, param, message);
	}

	public static void info(Class<?> clazz, String cornExpression, Map<?, ?> param, Object... message) {
		log("info", cornExpression, clazz, param, message);
	}

	private static String getStackTrace(Throwable t) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw, true);
		t.printStackTrace(pw);
		pw.flush();
		pw.close();
		sw.flush();
		String trace = sw.toString();
		try {
			sw.close();
		} catch (IOException e) {
			GGLogger.error(GGServiceJob.class, e);
		}
		if (StringUtils.isNotBlank(trace)) {
			trace = trace.replaceAll("\\r\\n\\t", ";  ");
			trace = trace.replaceAll("\\r\\n", ";  ");
			trace = trace.replaceAll("[\\t]", "\t");
		}
		return trace;
	}
}
